﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuadRemote.Core;

namespace QuadRemote
{
    public class RawSDReadFrame : GroupBox
    {
        TextBox tb;
        TextBox sector;

        public RawSDReadFrame()
        {
            Text = "Raw SD Read";

            Button bt = new Button();
            bt.Text = "Get";
            bt.Location = new Point(200, 30);
            bt.Size = new Size(100, 25);
            bt.Click += new EventHandler(RequestRaw);
            Controls.Add(bt);

            bt = new Button();
            bt.Text = "Format";
            bt.Location = new Point(320, 30);
            bt.Size = new Size(100, 25);
            bt.Click += new EventHandler(SdFormat);
            Controls.Add(bt);

            tb = new TextBox();
            tb.Multiline = true;
            tb.Font = new Font("Courier New", 9);
            tb.Location = new Point(10, 60);
            tb.Size = new Size(1200, 400);
            Controls.Add(tb);


            sector = new TextBox();
            sector.Text = "0";
            sector.Location = new Point(10, 30);
            sector.Size = new Size(150, 25);
            Controls.Add(sector);

            Bluetooth.instance().commandParsers[(int)CommandId.CMD_SD_RAW_READ] = incomingRawPacket;

            Size = new Size(1280, 480);
        }

        private void RequestRaw(object sender, EventArgs args)
        {
            int val = int.Parse(sector.Text);
            new CommandOut(CommandId.CMD_SD_RAW_READ, (byte)(val >> 24), (byte)(val >> 16), (byte)(val >> 8), (byte)(val));
        }

        private void SdFormat(object sender, EventArgs args)
        {
            int val = int.Parse(sector.Text);

            new CommandOut(CommandId.CMD_SD_FORMAT);
            //Core.Bluetooth.instance().sendPriorityPacket((byte)Bluetooth.CMD_ID.CMD_SD_FORMAT);
        }

        private delegate void packetEventDelegate(CommandIn args);

        private void incomingRawPacket(CommandIn args)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new packetEventDelegate(incomingRawPacket), new object[] { args });
                return;
            }
            String s = "";
            int sector = Tools.bytesToInt32(args.packet, 0);

            s += "Sector: " + sector + "\r\n\r\n";

            s += "     ";
            for (int i = 0; i < 32;i++)
            {
                s += i + (i < 10 ? " " : "") + " ";
            }
            s += "   0         10        20        30\r\n";
            s += "   +";
            for (int i = 0; i < 32 * 3 + 4 + 32; i++ )
                s += (i == 32 * 3 + 4 || i == 32 * 3 + 4 + 10 || i == 32 * 3 + 4 + 20 || i == 32 * 3 + 4 + 30) ? "+" : "-";
            s += "\r\n";
            for (int i = 0; i < 512; i += 32)
            {
                s += (i < 10 ? " " : "") + (i < 100 ? " " : "") + i + "| " + Tools.getHexString(args.packet, i + 4, 32);
                s += "    ";

                for (int j = i; j < i + 32; j++)
                {
                    if (args.packet[j + 4] >= 32 && args.packet[j + 4] != 127 && args.packet[j + 4] != 255)
                        s += new String((char)args.packet[j + 4], 1);
                    else
                        s += ".";
                }

                s += "\r\n";
            }


            tb.Text = s;
        }
    }
}
